/**
 * *****************************************************************************
 * @file    fft.c
 * @author  Jack
 * @version 1.0.0
 * @date    2022-08-26
 * @brief
 *
 *                 Copyright (c) 2021, Andar Technologies Inc.
 *                           www.andartechs.com
 *
 * *****************************************************************************
 */
/*------------------------------ include -------------------------------------*/
#include "fft.h"
#include "arm_math.h"
#include "adt3102.h"
#include "adt3102_dsp.h"
#include "static_remove.h"
#include "adt3102_sagc.h"
#include "adt3102_rx.h"
#include "adt3102_adc.h"
#include "adt3102_pmu.h"
#include "adt3102_memscheduling.h"

/**
 * @brief   Send chirp and collect ADC.
 * @param   dspMemoryStgrTypedef.
 *          See the @dspMemoryStgrTypedef member for details.
 * @return  None.
 * @note
 */
void doSendChirp(dspMemoryStgrTypedef *memory)
{
#if TXRXTYPE == TX1RX1
    adcSampleStart(ADC0SEL, memory->samplePoint, memory->chirpNumCfg,   \
        ADC0_FIFO, memory->srcADCR0,  \
        ADC1_FIFO, memory->srcADCR1);
#elif TXRXTYPE == TX1RX2
    adcSampleStart(ADC01SEL, memory->samplePoint, memory->chirpNumCfg,   \
        ADC0_FIFO, memory->srcADCR0,  \
        ADC1_FIFO, memory->srcADCR1);
#elif TXRXTYPE == TX2RX2
    adcSampleStart(ADC01SEL, memory->samplePoint, memory->chirpNumCfg,   \
        ADC0_FIFO, memory->srcADCR0,  \
        ADC1_FIFO, memory->srcADCR1);
#endif
}

/**
 * @brief   Calculate the gain of tia and vag.
 * @param   AnalogDataStrTypedef.
 *          See the @AnalogDataStrTypedef member for details.
 * @param   dspMemoryStgrTypedef.
 *          See the @dspMemoryStgrTypedef member for details.
 * @return  None.
 * @note
 */
void doAGC(AnalogDataStrTypedef *pDataStr, dspMemoryStgrTypedef *memory)
{
#if SAGC_EN
    #if WN_BYPASS
        uint32 sagcTarget=800;
    #else
        //xx. if 500, then the IF would be overflow. and for target within 30cm distance, there will be 2nd harmonic.
        uint32 sagcTarget=500;
    #endif
    
        sAgc(0, memory->srcADCR0, memory->samplePoint * 4, sagcTarget, \
                &pDataStr->tiaGain0, &pDataStr->pgaGain0);

        pDataStr->tiaGain1 = pDataStr->tiaGain0;
        pDataStr->pgaGain1 = pDataStr->pgaGain0;

        rfTiaGainSetting(pDataStr->tiaGain0, pDataStr->tiaGain0, \
                            pDataStr->tiaGain1, pDataStr->tiaGain1);
        rfVgaGainSetting(pDataStr->pgaGain0, pDataStr->pgaGain0, \
                            pDataStr->pgaGain1, pDataStr->pgaGain1);
#endif
}
